import Reflux from 'reflux';
import Actions from "./Actions";
import update from 'react-addons-update';
import DB from '../../../../../app/db';
import StorageUtils from "../../../../../app/storage";
import Utils from "../../../../../utils/index";

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    userInfo: {},
    xq: []
  },

  init() {
    // this.data.userInfo = update(this.data.userInfo, {$set: StorageUtils.getUser()});
    // this.updateComponent();
  },

  getXiaoqu() {
    this.data.userInfo = update(this.data.userInfo, {$set: StorageUtils.getUser()});
    this.updateComponent();
    let t = this;
    DB.ywh.getXiaoqu({proid: StorageUtils.getUser().proprietorId}).then(
      function (content) {
        if (content && content.length > 0) {
          let xq = [];
          content.map(d => {
            xq.push(d);
          });
          t.data.xq = update(t.data.xq, {$set: xq});
          t.updateComponent();
        }
      },
      function (error) {
        console.log(error);
      }
    );
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState() {
    return this.data;
  }
});
